/*
 * Copyright (c) 2008 Proxy Wars Community.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Proxy Wars Community' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.proxywars.engine.game;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.proxywars.engine.game.state.IngameState;
import org.proxywars.engine.game.state.EnumProxyWarsGameState;
import org.proxywars.engine.input.handler.IngameHandler;

import com.jme.app.BaseGame;
import com.jme.system.DisplaySystem;
import com.jme.system.JmeException;
import com.jme.util.Timer;
import com.jmex.game.state.GameStateManager;

/**
 * <code>ProxyWarsGame</code> defines the application's entry point and the
 * implementation for the abstract methods in <code>BaseGame</code>.
 * <p>
 * Uses a <code>Timer</code> to calculate the time per frame to be passed to the active <code>GameState</code>.
 * 
 * @see BaseGame
 * 
 * @author Alejandro Duarte.
 * 
 */
public class ProxyWarsGame extends BaseGame {
	
	private static Logger logger = Logger.getLogger(ProxyWarsGame.class.getName());
	protected Timer timer;		/** High resolution timer used to calculate time per frame (tpf). */
	protected float tpf;		/** Time per frame. */
	
	/**
	 * Application's entry point.
	 * 
	 * @param args
	 *            Command line arguments (not currently used).
	 */
	public static void main(String[] args) {
		ProxyWarsGame game = new ProxyWarsGame();
		game.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
		game.start();
	}

	@Override
	protected void initSystem() {
		try {
			display = DisplaySystem.getDisplaySystem(properties.getRenderer());
			display.createWindow(
					properties.getWidth(),
					properties.getHeight(),
					properties.getDepth(),
					properties.getFreq(),
					properties.getFullscreen()
			);
			display.setTitle("Proxy Wars - Prototype 0.0.1");
		} catch (JmeException e) {
			logger.log(Level.SEVERE, "Could not create displaySystem.", e);
			System.exit(1);
		}
		timer = Timer.getTimer();
	}

	@Override
	protected void initGame() {
		GameStateManager.create();
		setupGameStates();
		GameStateManager.getInstance().activateChildNamed(EnumProxyWarsGameState.INLEVEL.toString());
	}
	
	@Override
	protected void cleanup() {
		GameStateManager.getInstance().cleanup();
	}
	
	@Override
	protected void reinit() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void update(float interpolation) {
		timer.update();
		tpf = timer.getTimePerFrame();
		GameStateManager.getInstance().update(tpf);
	}
	
	@Override
	protected void render(float interpolation) {
		display.getRenderer().clearBuffers();
		GameStateManager.getInstance().render(tpf);
	}
	
	/**
	 * Creates and attaches all the game states needed by this game.
	 */
	protected void setupGameStates() {
		GameStateManager.getInstance().deactivateAllChildren();
		GameStateManager.getInstance().detachAllChildren();
		GameStateManager.getInstance().attachChild(
				new IngameState(
						EnumProxyWarsGameState.INLEVEL.toString(),
						new IngameHandler()
				)
		);
	}
}
